import { Component, OnInit } from '@angular/core';
import { User } from '../../domain/user';
import { ManagerService } from '../../service/manager.service';
import { LeaveRequest } from '../../domain/leave-request';
import { DepartmentManagerDealPost } from '../../domain/department-manager-deal-post';
import { DeputyManagerDealPost } from '../../domain/deputy-manager-deal-post';
import { MainManagerDealPost } from '../../domain/main-manager-deal-post';

import { AuthService } from '../../service/auth.service';
/*

@Author:李朝晖

@Date: 2018-12-12

@经理处理请假信息功能实现

@Last Modified time: 2018-12-16
*/
@Component({
  selector: 'app-manager-deal-leave',
  templateUrl: './manager-deal-leave.component.html',
  styleUrls: ['./manager-deal-leave.component.css']
})
export class ManagerDealLeaveComponent implements OnInit {
  private leaveRequest: LeaveRequest[];
  private leave: LeaveRequest;
  private user: User;//查看详情的员工
  private users: User[];
  private manager: User;
  private suggest: string;
  private myDate;
  private suggestion = null;

  constructor(private managerService: ManagerService) {
    this.user = {
      id: 0,
      user_name: null,
      name: null,
      age: 0,
      sex: null,
      department: null, //部门
      position: null, //职务
      annual_leave_length: null, //年假上限
      remain_annual_leave: null, //剩余年假
    };
    this.manager = {
      id: null,
      user_name: null,
      name: null,
      age: null,
      sex: null,
      department: null, //部门
      position: null, //职务
      annual_leave_length: null, //年假上限
      remain_annual_leave: null, //剩余年假
    };
    this.myDate = new Date();
  }

  ngOnInit() {
    this.getUser();
  }

  getUser() {
    var id = AuthService.getUserId();
    if (id == 0) {
      alert("请先登录");
    } else {
      this.managerService.getUser(id)
        .subscribe(manager => {
          this.manager = manager[0];
          this.getLeaveRequest();
          this.getUsers();
        });
    }
  }

  //获取所有用户信息用于部门部门判定
  getUsers() {
    this.managerService.getUsers()
      .subscribe(users => {
        this.users = users;
      });
  }
  getLeaveRequest() {
    if (this.manager.position == "部门经理") {
      this.managerService.getDepartmentLeaveRequest(this.manager.department)
        .subscribe(leaveRequest => {
          this.leaveRequest = leaveRequest;
          //按时间排序
          for (var i = 0; i < this.leaveRequest.length - 1; i++) {
            if (this.leaveRequest[i].start_time < this.leaveRequest[i + 1].start_time) {
              var temp: LeaveRequest = this.leaveRequest[i];
              this.leaveRequest[i] = this.leaveRequest[i + 1];
              this.leaveRequest[i + 1] = temp;
              i = 0;
            }
          }
        });
    } else {
      this.managerService.getAllLeaveRequest()
        .subscribe(leaveRequest => {
          this.leaveRequest = leaveRequest;
          //按时间排序
          for (var i = 0; i < this.leaveRequest.length - 1; i++) {
            if (this.leaveRequest[i].start_time < this.leaveRequest[i + 1].start_time) {
              var temp: LeaveRequest = this.leaveRequest[i];
              this.leaveRequest[i] = this.leaveRequest[i + 1];
              this.leaveRequest[i + 1] = temp;
              i = 0;
            }
          }
        });
    }

  }

  getLeaveDetail(leave: LeaveRequest) {
    this.managerService.getUser(leave.user_id)
      .subscribe(user => this.user = user[0]);
    this.leave = leave;
  }

  onConfirm() {
    if (this.manager.position == "部门经理") {
      if (this.leave.state != 2 || this.user.department != this.manager.department) {
        alert("当前状态您没有权限进行更改！");
        return;
      } else {
        this.departmentManagerDeal(1);
      }
    } else if (this.manager.position == "副总经理") {
      if (this.leave.state != 3) {
        alert("当前状态您没有权限进行更改！");
        return;
      } else {
        this.deputyManagerDeal(1);
      }
    } else if (this.manager.position == "总经理") {
      if (this.leave.state != 4) {
        alert("当前状态您没有权限进行更改！");
        return;
      } else {
        this.mainManagerDeal(1);
      }
    } else {
      alert("不好意思，请先登录才能进行操作...");
    }
  }

  onRefuse() {
    if (this.manager.position == "部门经理") {
      if (this.leave.state != 2 || this.user.department != this.manager.department) {
        alert("当前状态您没有权限进行更改！");
        return;
      } else {
        this.departmentManagerDeal(0);
      }
    } else if (this.manager.position == "副总经理") {
      if (this.leave.state != 3) {
        alert("当前状态您没有权限进行更改！");
        return;
      } else {
        this.deputyManagerDeal(0);
      }
    } else if (this.manager.position == "总经理") {
      if (this.leave.state != 4) {
        alert("当前状态您没有权限进行更改！");
        return;
      } else {
        this.mainManagerDeal(0);
      }
    } else {
      alert("不好意思，请先登录才能进行操作...");
    }
  }

  departmentManagerDeal(TOrF: number) {
    if (TOrF == 0) {
      this.leave.state = 0;
    } else {
      this.leave.state = 3;
    }
    if (this.leave.suggestion == null) {
      this.leave.suggestion = "\n" + this.manager.position + " " + this.manager.name + ":\n" + this.suggest + "\n";
    } else {
      this.leave.suggestion = this.leave.suggestion + "\n" + this.manager.position + " " + this.manager.name + ":\n" + this.suggest + "\n";
    }

    this.managerService.updateLeaveRequest(this.leave)
      .subscribe((data) => console.log(data));

    var dealPost: DepartmentManagerDealPost = {
      department_manager_id: this.manager.id,
      request_id: this.leave.id,
      request_type: 1, //0外出，1请假
      approval_time: this.myDate.toLocaleString(),
      suggestion: this.suggest,
      state: TOrF,
    }
    this.managerService.createDepartmentManagerDeal(dealPost)
      .subscribe((data) => {
        this.getLeaveRequest();
        alert("审批完成！");
      });
  }

  deputyManagerDeal(TOrF: number) {
    if (TOrF == 0) {
      this.leave.state = 0;
    } else {
      if (this.leave.duration <= 3) {
        this.leave.state = 1;
      } else {
        this.leave.state = 4;
      }
    }
    if (this.leave.suggestion == null) {
      this.leave.suggestion = "\n" + this.manager.position + " " + this.manager.name + ":\n" + this.suggest + "\n";
    } else {
      this.leave.suggestion = this.leave.suggestion + "\n" + this.manager.position + " " + this.manager.name + ":\n" + this.suggest + "\n";
    }
    this.managerService.updateLeaveRequest(this.leave)
      .subscribe((data) => console.log(data));

    var dealPost: DeputyManagerDealPost = {
      deputy_manager_id: this.manager.id,
      request_id: this.leave.id,
      request_type: 1, //0外出，1请假
      approval_time: this.myDate.toLocaleString(),
      suggestion: this.suggest,
      state: TOrF,
    }
    this.managerService.createDeputyManagerDeal(dealPost)
      .subscribe((data) => {
        console.log(data);
        this.getLeaveRequest();
        alert("审批完成！");
      });
  }

  mainManagerDeal(TOrF: number) {
    if (TOrF == 0) {
      this.leave.state = 0;
    } else {
      this.leave.state = 1;
    }
    if (this.leave.suggestion == null) {
      this.leave.suggestion = "\n" + this.manager.position + " " + this.manager.name + ":\n" + this.suggest + "\n";
    } else {
      this.leave.suggestion = this.leave.suggestion + "\n" + this.manager.position + " " + this.manager.name + ":\n" + this.suggest + "\n";
    }
    this.managerService.updateLeaveRequest(this.leave)
      .subscribe((data) => console.log(data));

    var dealPost: MainManagerDealPost = {
      main_manager_id: this.manager.id,
      request_id: this.leave.id,
      request_type: 1, //0外出，1请假
      approval_time: this.myDate.toLocaleString(),
      suggestion: this.suggest,
      state: TOrF,
    }
    this.managerService.createMainManagerDeal(dealPost)
      .subscribe((data) => {
        this.getLeaveRequest();
        alert("审批完成！");
      });
  }

  onSelect(): void {
    let temp = [];
    for (var i = 0; i < this.leaveRequest.length; i++) {
      if (this.manager.position == "部门经理") {
        if (this.leaveRequest[i].state == 2 && this.onSameDepartment(this.leaveRequest[i].user_id)) {
          temp.push([].concat(this.leaveRequest[i]));
        }
      } else if (this.manager.position == "副总经理") {
        if (this.leaveRequest[i].state == 3) {
          temp.push([].concat(this.leaveRequest[i]));
        }
      } else if (this.manager.position == "总经理") {
        if (this.leaveRequest[i].state == 4) {
          temp.push([].concat(this.leaveRequest[i]));
        }
      } else {
        alert("未知职位登录：" + this.manager.position);
      }
    }
    for (var i = 0; i < temp.length; i++) {
      this.leaveRequest[i] = [].concat(temp[i])[0];
    }
    this.leaveRequest.splice(temp.length, this.leaveRequest.length - temp.length);
  }

  onSameDepartment(id: number) {
    for (var i = 0; i < this.users.length; i++) {
      if (this.users[i].id == id) {
        //如果部门相同
        if (this.users[i].department == this.manager.department) {
          return true;
        }
        return false;
      }
    }
    return true;
  }

  lookSuggestion(suggestion: string) {
    this.suggestion = suggestion;
  }

  backSuggestion() {
    this.suggestion = null;
  }
  getState(num: number) {
    if (num == 0)
      return "拒绝请假";
    else if (num == 1)
      return "已通过";
    else if (num == 2)
      return "等待部门经理";
    else if (num == 3)
      return "等待副总经理";
    else if (num == 4)
      return "等待总经理";
    else
      return "error"
  }
  getType(num: number) {
    if (num == 0)
      return "病假";
    else if (num == 1)
      return "婚假";
    else if (num == 2)
      return "事假";
    else if (num == 3)
      return "年假";
    else
      return "error"
  }
}
